/****************************************************************
 * Copyright (c), 2025, Shanghai Daimay Automotive Parts Co.,Ltd.
 * @file:    SystemTimer.c
 * @author:  Garry Meng
 * @version: V1.0.0  
 * @date:    2025-06-17
 * @brief:   system timer management
****************************************************************/
#include "../inc/SystemTimer.h"
        
SYS_TIMER_TYPE_t systemTimerValue;

void SystemTimer_init(void)
{
    systemTimerValue = 0u;
}

SYS_TIMER_TYPE_t SystemTimer_GetTimeValue(void)
{
    SYS_TIMER_TYPE_t t;

    t = systemTimerValue;
    
    return t;
}

SYS_TIMER_TYPE_t SystemTimer_GetTimeDelta(SYS_TIMER_TYPE_t lastCount)
{
    SYS_TIMER_TYPE_t ret;
    SYS_TIMER_TYPE_t t;

    // MCU_EnterCritical();
    t = systemTimerValue;
    // MCU_ExitCritical();

    if(lastCount <= t)
    {
        ret = (t - lastCount);
    }
    else
    {
        ret = (SYSTEM_TIMER_MAX - lastCount + t);
    }

    return ret;
}        

/*******************************************************************************
* Function Name: SystemTimer_callback
* Brief:This function should be set as the callback function for a certain timer
*  interrupt to realize the increasement of the system timer.
********************************************************************************/
void SystemTimer_callback( void )
{
    systemTimerValue ++;
}